﻿using IceDog.ElasticSearchClient.MSTest.Models;
using Nest;
using System;

namespace IceDog.ElasticSearchClient.MSTest.NEST
{
    public static class NESTClientProvider
    {
        private static ElasticClient _elasticClient;
        private static ElasticClient _elasticClientPool;

        public static ElasticClient GetElasticClient()
        {
            if (_elasticClient != null)
            {
                return _elasticClient;
            }

            var node = new Uri("http://localhost:9201");
            var settings = new ConnectionSettings(node);

            settings.RequestTimeout(TimeSpan.FromMinutes(2));

            //设置默认索引
            settings.DefaultIndex("tweet");
            //设置默认映射
            settings.DefaultMappingFor<Tweet>(d => d.IndexName("tweet"));
            
            var client = new ElasticClient(settings);
            _elasticClient = client;

            return _elasticClient;
        }

        public static ElasticClient GetElasticClientWithPool()
        {
            if (_elasticClientPool != null)
            {
                return _elasticClientPool;
            }
            var nodes = new Uri[]
            {
                new Uri("http://localhost:9201"),
                new Uri("http://localhost:9202")
            };
            var pool = new Elasticsearch.Net.StaticConnectionPool(nodes);
            var settings = new ConnectionSettings(pool);
            settings.RequestTimeout(TimeSpan.FromMinutes(2));
            var client = new ElasticClient(settings);
            _elasticClientPool = client;

            return _elasticClientPool;
        }
    }
}
